# -*- coding: utf-8 -*-
import comparable_wave
import logging

class WaveCatalog:
  def __init__(self, title, structure):
    self.title = title
    self.annotation = []
    self.wave_catalog = [] #sorted list of ComparableWaves
    self.structure = structure #tag hierarchy
       
  def __len__(self):
      return len(self.wave_catalog)
  
  def put(self, wave_id, title, tag_indexes):
    """ Puts ComparableWave to list and sorts this list. """
    cmp_wave = comparable_wave.ComparableWave(wave_id, title, tag_indexes, self.structure)
    self.wave_catalog.append(cmp_wave)
    self.wave_catalog.sort()
  
  def __unicode__(self):
    """ Returns unicode string and fills annotation list in format: 
          {'name': key, 
          "value": self.wave_catalog[i].wave_id, 
          "range": {"start": start, "end": end} } 
        for each cmp_wave. 
    """
    logging.warning("Catalog: %s, len(catalog): %d" % (self.title, len(self.wave_catalog)))
    key, tab, result_str = "link/wave", u"          ", u"\n\n\n"
    i = 0;
    print_titles = [ True for q in range(len(self.structure))] #0 - title must be printed. And all titles after 0 must be printed
    
    while i < len(self.wave_catalog):
      header = u""      
      j = 0 #title must be printed between 1.1 + 3.1 and 1.1 + 2.2, lvl2 
      if i > 0:
        while (j < self.wave_catalog[i].end + 1) \
                and self.wave_catalog[i].tag_indexes[j] == self.wave_catalog[i - 1].tag_indexes[j]:
          j += 1 #length of missing tags. these tags have been printed       
      str_len = len(result_str)
      for k in range(j, self.wave_catalog[i].end + 1): #printing tags, k means a shift for current tag and place in tag's levels
        if self.wave_catalog[i].tag_indexes[k] < len(self.structure[k]):
          if print_titles[k]: #do we need level's title:
            begin = str_len + len(header)
            header += k * tab + self.structure[k].name + "\n"
            end = str_len + len(header) - 1
            self.annotation.append({'name': 'style/color', 
                                    'value': 'rgb(153, 51, 229)', 
                                    'range': {'start': begin, 'end': end }})
            self.annotation.append({'name': 'style/fontStyle', 
                                    'value': 'italic', 
                                    'range': {'start': begin, 'end': end }})
            #self.annotation.append({'name': 'style/fontSize', 
                                    #'value': '14pt', 
                                    #'range': {'start': begin, 'end': end }})
            print_titles[k] = False #we have printed
          header += k * tab + self.structure[k][self.wave_catalog[i].tag_indexes[k]] + u"\n" #ppc
      if len(header) > 0:
          self.annotation.append({'name': 'style/fontWeight', 
                                  'value': 'bold', 
                                  'range': {'start': str_len, 'end': str_len + len(header)}})
      result_str += header
      
      result_str += self.wave_catalog[i].end * tab + "          " 
      begin = len(result_str) 
      result_str += unicode(self.wave_catalog[i]) + "\n" #there is at least one wave wave in this category
      end = len(result_str) - 1
      self.annotation.append({'name': key, 
                              "value": self.wave_catalog[i].wave_id, 
                              "range": {'start': begin, "end": end} })
      i += 1
      while i < len(self.wave_catalog) and self.wave_catalog[i - 1] == self.wave_catalog[i]: #wave's output
        result_str += self.wave_catalog[i].end * tab + "          "
        begin = len(result_str) 
        result_str += unicode(self.wave_catalog[i]) + "\n"
        end = len(result_str) - 1
        self.annotation.append({'name': key, 
                                "value": self.wave_catalog[i].wave_id, 
                                "range": {'start': begin, "end": end}})
        i+=1
      #(i-1)wave has been added, i - is next wave
      
      #correcting future title output:
      if i < len(self.wave_catalog):
        if self.wave_catalog[i].end < self.wave_catalog[i - 1].end:
          for q in range(self.wave_catalog[i].end + 1, len(self.structure)):
            print_titles[q] = True
        if self.wave_catalog[i - 1].begin < self.wave_catalog[i].begin:
          print_titles = [ True for q in range(len(self.structure))] #must be reseted between levels
          separate_str = u'Волны без тегов %s' % self.structure[0].name
          lvl_idx = 1          
          while lvl_idx < self.wave_catalog[i].begin:
              separate_str += u' и %s' % self.structure[lvl_idx].name
          separate_str += '\n'
          begin = len(result_str)
          result_str += separate_str
          self.annotation.append({'name': 'style/fontWeight', 
                                  'value': 'bold', 
                                  'range': {'start': begin, 'end': len(result_str) - 1}})          
    #result_str = result_str[:-1]
    return result_str